package com.guofen.controller.sjq;

import com.guofen.pojo.SjqWaterProductionFailure;
import com.guofen.service.sjq.SjqWaterProductionFailureService;
import com.guofen.util.result.Result;
import com.guofen.util.result.ResultGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("/sjq/SjqWaterProductionFailure")
@Controller
@Api(tags = "SjqWaterProductionFailureController", description = "水处理生产现场记录表（设备故障)")
public class SjqWaterProductionFailureController {
    @Autowired
    private SjqWaterProductionFailureService sjqWaterProductionFailureService;

    @ApiOperation("添加记录")
    @RequestMapping(value = "/addForm", method = RequestMethod.POST)
    @ResponseBody
    public Result addForm(@ApiParam(name = "equipment_name", value = "设备名称", required = true) @RequestParam String equipment_name,
                          @ApiParam(name = "failure_reason", value = "故障原因分析", required = true) @RequestParam String failure_reason,
                          @ApiParam(name = "maintenance_method", value = "维修方法", required = true) @RequestParam String maintenance_method,
                          @ApiParam(name = "recovery_time", value = "故障恢复时间", required = true) @RequestParam String recovery_time,
                          @ApiParam(name = "examiner", value = "检查人", required = true) @RequestParam String examiner,
                          @ApiParam(name = "down_time", value = "时间", required = true) @RequestParam String down_time,
                          @ApiParam(name = "operate_person", value = "操作人", required = true) @RequestParam String operate_person,
                          @ApiParam(name = "repair_person", value = "维修人员名字", required = false) @RequestParam String repair_person,
                          @ApiParam(name = "failure_description", value = "故障情况描述", required = false) @RequestParam String failure_description){

        if (sjqWaterProductionFailureService.addForm(equipment_name, failure_reason, maintenance_method, recovery_time,
                examiner, down_time, operate_person, repair_person, failure_description))
            return ResultGenerator.genSuccessResult();
        else
            return ResultGenerator.genFailResult("保存失败");
    }

    @ApiOperation("查询记录")
    @RequestMapping(value = "/findForm", method = RequestMethod.GET)
    @ResponseBody
    public Result findForm(){
        if (sjqWaterProductionFailureService.findForm() != null)
            return ResultGenerator.genSuccessResult(sjqWaterProductionFailureService.findForm());
        else
            return ResultGenerator.genFailResult("暂无记录");
    }


    @ApiOperation("删除记录")
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public Result delete(@ApiParam(name="id",value="ID",required=true) @RequestParam Integer id) {
        return ResultGenerator.genSuccessResult(sjqWaterProductionFailureService.delete(id));
    }

    @ApiOperation("修改记录")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public Result update(@ApiParam(name="id",value="ID",required=true) @RequestParam Integer id,
                         @ApiParam(name = "equipment_name", value = "设备名称", required = true) @RequestParam String equipment_name,
                         @ApiParam(name = "failure_reason", value = "故障原因分析", required = true) @RequestParam String failure_reason,
                         @ApiParam(name = "maintenance_method", value = "维修方法", required = true) @RequestParam String maintenance_method,
                         @ApiParam(name = "recovery_time", value = "故障恢复时间", required = true) @RequestParam String recovery_time,
                         @ApiParam(name = "examiner", value = "检查人", required = true) @RequestParam String examiner,
                         @ApiParam(name = "down_time", value = "时间", required = true) @RequestParam String down_time,
                         @ApiParam(name = "operate_person", value = "操作人", required = true) @RequestParam String operate_person,
                         @ApiParam(name = "repair_person", value = "维修人员名字", required = false) @RequestParam String repair_person,
                         @ApiParam(name = "failure_description", value = "故障情况描述", required = false) @RequestParam String failure_description) {
        SjqWaterProductionFailure record = new SjqWaterProductionFailure();
        record.setId(id);
        record.setEquipmentName(equipment_name);
        record.setFailureReason(failure_reason);
        record.setMaintenanceMethod(maintenance_method);
        record.setRecoveryTime(recovery_time);
        record.setExaminer(examiner);
        record.setDownTime(down_time);
        record.setOperatePerson(operate_person);
        record.setRepairPerson(repair_person);
        record.setFailureDescription(failure_description);
        return ResultGenerator.genSuccessResult(sjqWaterProductionFailureService.update(record));
    }

    @ApiOperation("查找记录")
    @RequestMapping(value = "/select",method = RequestMethod.POST)
    @ResponseBody
    public Result select(@ApiParam(name="pageSize",value="页容量",required=true) @RequestParam Integer pageSize,
                         @ApiParam(name="currentPage",value="当前页",required=true) @RequestParam Integer currentPage,
                         @ApiParam(name="startTime",value="开始时间") Long startTime,
                         @ApiParam(name="endTime",value="截止时间") Long endTime) {
        if (startTime == null || endTime == null)
            return ResultGenerator.genSuccessResult(sjqWaterProductionFailureService.get(pageSize, currentPage));
        else
            return ResultGenerator.genSuccessResult(sjqWaterProductionFailureService.getByTime(pageSize, currentPage, startTime, endTime));
    }
}
